package com.education.online.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.ApiReturnObject;
import com.education.online.pojo.OCatalogMenu;
import com.education.online.pojo.OFreeClass;
import com.education.online.service.OCatalogMenuService;
import com.education.online.service.OFreeClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 免费公开课表
 *
 * @date 2020-06-04 15:17:49
 */
@Api(tags = {"PC--免费公开课表"})
@RestController
@RequestMapping("/online/ofreeclass")
public class OFreeClassController {

    @Autowired
    private OFreeClassService oFreeClassService;

    @Autowired
    private OCatalogMenuService oCatalogMenuService;

    @ApiOperation(value = "查询全部免费公开课以及详情", httpMethod = "GET")
    @GetMapping("/selectList")
    public ApiReturnObject selectList(Integer pageNum, Integer pageSize,Integer freeId) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        OFreeClass oFreeClass = new OFreeClass();
        oFreeClass.setId(freeId);
        List<OFreeClass> classList = oFreeClassService.selectOFreeClassList(oFreeClass);
        PageInfo<OFreeClass> pageInfo = new PageInfo<>(classList);
        return new ApiReturnObject("200", "success", pageInfo);
    }

    @ApiOperation(value = "根据课程名称搜索--模糊查询", httpMethod = "GET")
    @GetMapping("/search")
    public ApiReturnObject search(String name) {
        List<OFreeClass> classList = oFreeClassService.selectOFreeClassByName(name);
        return new ApiReturnObject("200", "success", classList);
    }


    @ApiOperation(value = "免费公开课目录筛选", httpMethod = "GET")
    @GetMapping("/freeClassFilter")
    public ApiReturnObject freeClassFilter(Integer menuId) {
        List<OCatalogMenu> oCatalogMenuList = oCatalogMenuService.selectOCatalogMenuByParentId(menuId);
        //判断该目录是否是一级目录 未查询到  说明是二级目录  否则是一级
        if (oCatalogMenuList.size() == 0){
            //查询二级目录下的所有课程
            List<Integer> list = new ArrayList<>();
            list.add(menuId);
            List<OFreeClass> freeClassList = oFreeClassService.selectOFreeClassByMenuIds(list);
            return new ApiReturnObject("200", "success", freeClassList);
        }else {
            //查询一级目录下的所有课程
            List<Integer> arrayList = new ArrayList<>();
            for (OCatalogMenu oCatalogMenu : oCatalogMenuList) {
                arrayList.add(oCatalogMenu.getId());
            }
            List<OFreeClass> freeClassList = oFreeClassService.selectOFreeClassByMenuIds(arrayList);
            return new ApiReturnObject("200", "success", freeClassList);
        }
    }

    /**
     * feign : 根据id查询免费公开课
     */
    @GetMapping("/selectFreeClassById")
    public Map<String, Object> selectFreeClassById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        OFreeClass oFreeClass = oFreeClassService.selectOFreeClassById(id);
        map.put("name",oFreeClass.getRName());//名称
        map.put("url",oFreeClass.getRUrl());//资源路径
        map.put("image",oFreeClass.getRCover());//封面
        return map;
    }
}
